package com.whity.towerdefense;

import com.whity.towerdefense.R;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.TextView;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;

import com.whity.towerdefense.controler.GameControler;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.view.GameView;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class TowerDefenseActivity extends Activity
{
	// =============================================================
	// Public attributs
	
	public static TowerDefenseActivity	ACTIVITY;
	
	// =============================================================
	// Private attributs :
	
	private static final	String			TAG = "com.djm.test03";
	
	//private ActionPhaseView m_APView;
	private 				GameModel 		m_GameModel;
	private 				GameControler	m_GameControler;
	private 				GameView		m_GameView;
	
	// =============================================================
	// Public functions :
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle p_SavedInstanceState)
    {
        super.onCreate(p_SavedInstanceState);
        super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        ACTIVITY = this;
        Utils.TRACKER = GoogleAnalyticsTracker.getInstance();
        Utils.TRACKER.startNewSession(Utils.ID_GOOGLE_ANALYTICS, this);
        
        AssetManager assetManager = getAssets();
        // Instanciate Model :
        m_GameModel = new GameModel(assetManager);
        
        // Instanciate Controller :
        m_GameControler = new GameControler(m_GameModel);
        
        // Instanciate a View representing the model
        m_GameView = new GameView(assetManager, this, m_GameModel, m_GameControler);
        
        // Shared preference
        Utils.BACKEDUP_DATA = getSharedPreferences("BackedUpDatas", MODE_PRIVATE);
        Utils.LOCAL_DATA = getSharedPreferences("LocalDatas", MODE_PRIVATE);
        
       
        // Remove notification bar and title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.main);
        
        // Add button listener
        Button button = (Button)findViewById(R.id.MainMenuStartCampainButton);
        button.setOnClickListener(m_GameControler);
        
    }
    
    public void ChangeLayout(int p_ID)
    {
    	TowerDefenseActivity.ACTIVITY.setContentView(p_ID);
    }
    
    // =============================================================
    // Protected functions :
    
    @Override
    protected void onPause()
    {
        super.onPause();
        
        // Stop the tracker when it is no longer needed.
        if(!Utils.TRACKER.dispatch())
        {
      	  Log.w(TAG, "Can't dispatch Google analytics Event");
        }
        
        m_GameControler.OnPause();
        //mGLView.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        
        m_GameControler.OnResume();
        
        //mGLView.onResume();
    }
    
    @Override
    protected void onStart()
    {
    	super.onStart();
    }
    
    @Override
    protected void onDestroy()
    {
      super.onDestroy();
      // Stop the tracker when it is no longer needed.
      if(!Utils.TRACKER.dispatch())
      {
    	  Log.w(TAG, "Can't dispatch Google analytics Event");
      }
      Utils.TRACKER.stopSession();
    }
    
    // =============================================================
}